$(document).ready(function(){
	console.log('document ready');
        $.ajax({url: 'http://leoto.us/holiday/test/product.php',
            type: 'get',
             // Convert a form to a JSON string representation                     
                    async: true,
                    dataType: "json",
                    beforeSend: function() {
                        console.log('beforeSend');
                    },
                    complete: function() {       
 //                       console.log($(".box"));
			$(".box").click(function(){
                            pageModel.product = this.id;
                        });	
                    },
                    success: function (result) {
                        $("#Menu").html("Menu");
               //         document.getElementById("Menu").innerHTML = "Menu";
                        console.log(result);
                //	console.log(result.product_id);
                        for (var i = 0; i < result.length; i++)
                        {   
                            if (result[i].image_url[0]===null){
                                result[i].image_url[0]="img/noimg.png";
                            }
                            result[i].price = Number(result[i].price).toFixed(1);
                            for (var j = 0; j < result[i].category_ids.length; j++){
                                if (result[i].category_ids[j]==='5'){
                                    $("#recomment").append("<li><a href=\"#pagetwo\"><div class=\"box\" id = \""+
                                            result[i].product_id+"\"><div class=\"frame\"><img src=\"" +
                                            result[i].image_url[0] +
                                            "\" class=\"item\"></div><div class=\"item_name\">" +
                                            result[i].name + "</div><div class=\"item_price\">$" + 
                                            result[i].price+"</div></div></a></li>");
                                }
                            }
//                                                  console.log(result[i].name);
                            $("#output").append("<li><a href=\"#pagetwo\"><div class=\"box\" id = \""+
                                    result[i].product_id+"\"><div class=\"frame\"><img src=\"" +
                                    result[i].image_url[0] +"\" class=\"item\"></div><div class=\"item_name\">" +
                                    result[i].name + "</div><div class=\"item_price\">$" +
                                    result[i].price+"</div></div></a></li>");
                        }
                        
                        console.log($(".box"));
                                             
						
                    },
                    error: function (request,error) {
                        // This callback function will trigger on unsuccessful action                
                        console.log(request);
			console.log(error);
                  }
                });   
                

                

});

var pageModel = {
    product : null
};

$(document).on("pageshow","#pagetwo",function(){
 //       pageModel.product = 5;
        $("#owl-demo").html("");
        $("#item_image").html("");
        $("#initem_name").html("");
        $("#initem_price").html("");
        $("#item_description").html("");
        $.ajax({url: 'http://leoto.us/holiday/test/product.php?product='+pageModel.product,
            type: 'get',
             // Convert a form to a JSON string representation                     
                    async: true,
                    dataType: "json",
                    beforeSend: function() {
                        console.log('beforeSend');

                    },
                    complete: function() {
                    },
                    success: function (result) {
                        var owl = $("#owl-demo");
                        owl.owlCarousel();
  
                        console.log(result);
    //                    i = pageModel.product;
                        if (result.image_url[0]===null){
                            result.image_url[0]="img/noimg.png";
                        }
                        for (i=0;i<result.image_url.length;i++){
                            content = "<div class=\"item\"><img src=\""+result.image_url[i]+"\"></div>";
 //                           $("#owl-demo").append("<div class=\"item\"><img src=\""+result.image_url[i]+"\"></div>");
                             owl.data('owlCarousel').addItem(content);
                        }
                        
 //                       if (result.image_url[0]===null){
 //                               result.image_url[0]="img/noimg.png";
//                        }
                        document.getElementById("Menu2").innerHTML = result.name;
                        result.price = Number(result.price).toFixed(1);
//                        $("#item_image").html("<img src=\""+result.image_url[0]+"\" id=\"initem\">");
                        $("#initem_name").html(result.name);
                        $("#initem_price").html(result.price);
                        $("#item_description").html(result.description);
                    },
                    error: function (request,error) {
                        // This callback function will trigger on unsuccessful action                
                        console.log(request);
			console.log(error);
                  }
                });  
});